package View;

import javax.swing.*;


import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import com.toedter.calendar.JDateChooser;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Calendar;

import Model.*;
import Controller.*;
import javax.swing.table.DefaultTableModel;

public class TelaMeta extends JPanel{
	GerenciadorFinancas gf = new GerenciadorFinancas();
	Meta me = new Meta();
	JTabbedPane meta;
	JPanel m;
	JScrollPane scrollPane;
	JComboBox tipo;
	JDateChooser inicio, fim;
	JSpinner pa, age;
	JFormattedTextField lucro;
	public JTable table_1;
	
	public TelaMeta() {
		setLayout(new GridLayout(0, 1, 0, 0));
		
		meta = new JTabbedPane(JTabbedPane.BOTTOM);
		m = new JPanel();
		meta.addTab("META", null, m, null);
		m.setBackground(null);
		
		scrollPane = new JScrollPane();
		scrollPane.getViewport().setBackground(new Color(100, 175, 239));
		
		GroupLayout gl_m = new GroupLayout(m);
		gl_m.setHorizontalGroup(
			gl_m.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_m.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane)
					.addContainerGap())
		);
		gl_m.setVerticalGroup(
			gl_m.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_m.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane)
					.addGap(31))
		);
		
		JPanel panel = new JPanel();
		panel.setBackground(null);
		scrollPane.setViewportView(panel);
		
		JLabel lblIniciarMetaEm = new JLabel("INICIAR EM");
		lblIniciarMetaEm.setHorizontalAlignment(SwingConstants.RIGHT);
		
		inicio = new JDateChooser();
		
		JLabel lblFinalizarEm = new JLabel("FINALIZAR EM");
		lblFinalizarEm.setHorizontalAlignment(SwingConstants.RIGHT);
		
		fim = new JDateChooser();
		
		JLabel lblAgendamentos = new JLabel("AGENDAMENTOS");
		lblAgendamentos.setHorizontalAlignment(SwingConstants.RIGHT);
		
		 age = new JSpinner();
		
		JLabel lblPacientes_1 = new JLabel("PACIENTES");
		lblPacientes_1.setHorizontalAlignment(SwingConstants.RIGHT);
		
		 pa = new JSpinner();
		
		JLabel lblLucroMnimo = new JLabel("LUCRO M\u00CDNIMO");
		lblLucroMnimo.setHorizontalAlignment(SwingConstants.RIGHT);
		
		 lucro = new JFormattedTextField();
		
		JLabel lblEstadoDaMeta_1 = new JLabel("META EM ANDAMENTO");
		lblEstadoDaMeta_1.setForeground(Color.RED);
		
		JLabel lblLegenda = new JLabel("LEGENDA:");
		
		JLabel lblMetaAlcanada = new JLabel("META ALCAN\u00C7ADA");
		lblMetaAlcanada.setForeground(Color.GREEN);
		
		JButton button = new JButton("OK");
		button.setBackground(new Color(255, 255, 255));
		button.setForeground(new Color(0, 175, 239));

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editar();
			}
		});
		
		tipo = new JComboBox();
		tipo.setModel(new DefaultComboBoxModel(new String[] {"DIARIA", "NORMAL"}));
		
		JLabel lblTipo = new JLabel("TIPO");
		lblTipo.setHorizontalAlignment(SwingConstants.RIGHT);
		
		
		JScrollPane scrollPane_1 = new JScrollPane();
		
		table_1 = new JTable();
		scrollPane_1.setViewportView(table_1);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(10)
							.addComponent(lblTipo, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
							.addGap(8)
							.addComponent(tipo, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
							.addGap(93)
							.addComponent(lblLegenda, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(10)
							.addComponent(lblIniciarMetaEm, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
							.addGap(8)
							.addComponent(inicio, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(93)
							.addComponent(lblEstadoDaMeta_1, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(10)
							.addComponent(lblFinalizarEm, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(fim, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(91)
							.addComponent(lblMetaAlcanada, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(10)
							.addComponent(lblPacientes_1, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(pa, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(10)
							.addComponent(lblAgendamentos, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
							.addGap(8)
							.addComponent(age, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(10)
							.addComponent(lblLucroMnimo, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
							.addGap(8)
							.addComponent(lucro, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(175)
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 572, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(189, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(37)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(3)
							.addComponent(lblTipo))
						.addComponent(tipo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(9)
							.addComponent(lblLegenda)))
					.addGap(5)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(lblIniciarMetaEm))
						.addComponent(inicio, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(lblEstadoDaMeta_1)))
					.addGap(8)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(lblFinalizarEm))
						.addComponent(fim, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(6)
							.addComponent(lblMetaAlcanada)))
					.addGap(14)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(3)
							.addComponent(lblPacientes_1))
						.addComponent(pa, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(8)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(3)
							.addComponent(lblAgendamentos))
						.addComponent(age, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(11)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(3)
							.addComponent(lblLucroMnimo))
						.addComponent(lucro, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(11)
					.addComponent(button, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
					.addGap(11)
					.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(23, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		m.setLayout(gl_m);
		add(meta);
		AtualizarTabela(gf.buscarMeta());
	}
	
	public void AtualizarTabela(ArrayList<Meta> al){
		
		Object[][] dados =new Object[al.size()][6];

		for (int i=0;i<al.size();i++){
			
			Meta p = al.get(i);

			Integer dia=p.inicio.get(Calendar.DAY_OF_MONTH);
			Integer mes=p.inicio.get(Calendar.MONTH);
			Integer ano =p.inicio.get(Calendar.YEAR);
			String inicio = dia.toString()+"/"+mes.toString()+"/"+ano.toString();
			
			Integer dia2=p.fim.get(Calendar.DAY_OF_MONTH);
			Integer mes2=p.fim.get(Calendar.MONTH);
			Integer ano2 =p.fim.get(Calendar.YEAR);
			String fim = dia2.toString()+"/"+mes2.toString()+"/"+ano2.toString();

			
			dados[i][0] = p.tipo; 
			dados[i][1] = inicio;
			dados[i][2] = fim;
			dados[i][3] = p.pacientes;
			dados[i][4] = p.agendamentos;
			dados[i][5] = p.lucro_m;
			}
		String[] nomesDasColunas = new String[] {
				"TIPO", "INICIAR", "FINALIZAR", "PACIENTES", "AGENDAMENTOS", "LUCRO M\u00CDNIMO"};
		
		DefaultTableModel tableModel = new DefaultTableModel(dados,nomesDasColunas);
		table_1.setModel(tableModel);
		
}

	
	public Meta getMeta(){
		me.tipo = tipo.getSelectedItem().toString();
		me.inicio = inicio.getCalendar();
		me.fim = fim.getCalendar();
		me.pacientes = Integer.parseInt(pa.getValue().toString());
		me.agendamentos = Integer.parseInt(age.getValue().toString());
		me.lucro_m = Integer.parseInt(lucro.getText());
		return me;
	}
	
	public void editar(){
		gf.editarMeta(getMeta());
		TelaPrincipal.AbrirFinancas();
	}
}